﻿//This file is part of Search Engine by Tomasz Kusienicki.
// 
//This Search Engine is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//any later version.
// 
//Foobar is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
// 
//You should have received a copy of the GNU General Public License
//on <http://www.gnu.org/licenses/>.

using System;
using System.Data.SqlClient;

namespace AdminApp1.DataSource
{
    public class Connection
    {

        #region Properties
        public SqlConnection DatabaseConnection { get; private set; }
        public bool IsConnected { get; private set; }

        private string _connectionString;
        public string ConnectionString
        {
            get { return _connectionString; }
            set 
            {
                _connectionString = value;
                DatabaseConnection = new SqlConnection(value);
            }
        }
        #endregion

        #region Connect, Disconnect
        public void Connect()
        {
            try
            {
                DatabaseConnection.Open();
                IsConnected = true;
            }
            catch (InvalidOperationException ioe) { Console.WriteLine(ioe.Message); }
            catch (SqlException se) { Console.WriteLine(se.Message); }
            catch (ArgumentException ae) { Console.WriteLine(ae.Message); }
        }

        public void Disconnect()
        {
            if (DatabaseConnection == null) return;
            DatabaseConnection.Dispose();
            DatabaseConnection = null;
            IsConnected = false;
        }
        #endregion

    }
}
